<?php

namespace backend\controllers;

use foo\bar;
use moonland\phpexcel\Excel;
use Yii;
use app\models\AsrCustomer;
use app\models\search\AsrCustomerSearch;
use yii\data\Pagination;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;

/**
 * AsrCustomerController implements the CRUD actions for AsrCustomer model.
 */
class AsrCustomerController extends CommonController
{
    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['POST'],
                ],
            ],
        ];
    }

    /**
     * Lists all AsrCustomer models.
     * @return mixed
     */
    public function actionIndex()
    {
        $searchModel = new AsrCustomerSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
        $this->pagesDo($dataProvider);

        return $this->render('index', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
        ]);
    }

    /**
     * Displays a single AsrCustomer model.
     * @param integer $id
     * @return mixed
     * @throws NotFoundHttpException if the model cannot be found
     * 全部用户
     */
    public function actionView($id)
    {

        $query = AsrCustomer::find()->select('asr_customer.customer_id,asr_customer.username,asr_customer.phone_num,asr_customer.company_name,asr_call_classify.call_number,asr_call_classify.call_time,asr_call_classify.created_at,asr_call_classify.updated_at')->joinWith('asrCallClassify',true,'LEFT JOIN')->where(['asr_customer.mobile_id'=>$id]);
//        var_dump($query->asArray()->all());
//        $commandQuery = clone $query;
//        echo $commandQuery->createCommand()->getRawSql();
//        exit;
        $countQuery = clone $query;
        $pages = new Pagination(['totalCount' => $countQuery->count()]);
        if (isset($_POST['pageSize'])) {//设置了参数，就把参数给pageSize session
            setcookie('pageSize', $_POST['pageSize']);
        }
        if (!empty($_COOKIE['pageSize'])) {
            $pages->pageSize = (int)$_COOKIE['pageSize'];
        } else {
            $pages->pageSize = 2;
        }

        $models = $query->offset($pages->offset)
            ->limit($pages->limit)->asArray()
            ->all();
        return $this->render('view', [
            'model' => $models,
            'pages' => $pages,
            'id'=>$id,
        ]);
    }

    //潜力用户
    public function actionPotnetial($id)
    {
        $query = AsrCustomer::find()->select('asr_customer.customer_id,asr_customer.username,asr_customer.phone_num,asr_customer.company_name,asr_call_classify.call_number,asr_call_classify.call_time,asr_call_classify.created_at,asr_call_classify.updated_at')->joinWith('asrCallClassify',true,'LEFT JOIN')->where(['asr_customer.mobile_id'=>$id])->where(['and' , ['>' , 'asr_call_classify.call_time' , 45] ]);
        $countQuery = clone $query;
        $pages = new Pagination(['totalCount' => $countQuery->count(), 'pageSize' => '1']);
        if (isset($_POST['pageSize'])) {//设置了参数，就把参数给pageSize session
            setcookie('pageSize', $_POST['pageSize']);
        }
        if (!empty($_COOKIE['pageSize'])) {
            $pages->pageSize = (int)$_COOKIE['pageSize'];
        } else {
            $pages->pageSize = 2;
        }
        $modelsq = $query->offset($pages->offset)
            ->limit($pages->limit)->asArray()
            ->all();
        return $this->render('potnetial', [
            'model' => $modelsq,
            'pages' => $pages,
            'id'=>$id,
        ]);

    }
    //普通用户
    public function actionCommon($id)
    {
        $query = AsrCustomer::find()->select('asr_customer.customer_id,asr_customer.username,asr_customer.phone_num,asr_customer.company_name,asr_call_classify.call_number,asr_call_classify.call_time,asr_call_classify.created_at,asr_call_classify.updated_at')->joinWith('asrCallClassify',true,'LEFT JOIN')->where(['asr_customer.mobile_id'=>$id])->where(['and' , ['>' , 'asr_call_classify.call_time' , 25],['<=','asr_call_classify.call_time' , 45]]);
        $countQuery = clone $query;
        $pages = new Pagination(['totalCount' => $countQuery->count(), 'pageSize' => '1']);
        if (isset($_POST['pageSize'])) {//设置了参数，就把参数给pageSize session
            setcookie('pageSize', $_POST['pageSize']);
        }
        if (!empty($_COOKIE['pageSize'])) {
            $pages->pageSize = (int)$_COOKIE['pageSize'];
        } else {
            $pages->pageSize = 2;
        }
        $modelsp = $query->offset($pages->offset)
            ->limit($pages->limit)->asArray()
            ->all();
        return $this->render('common', [
            'model' => $modelsp,
            'pages' => $pages,
            'id'=>$id,
        ]);

    }
    //拒绝用户
    public function actionDeny($id)
    {
        $query = AsrCustomer::find()->select('asr_customer.customer_id,asr_customer.username,asr_customer.phone_num,asr_customer.company_name,asr_call_classify.call_number,asr_call_classify.call_time,asr_call_classify.created_at,asr_call_classify.updated_at')->joinWith('asrCallClassify',true,'LEFT JOIN')->where(['and' , ['<=' , 'asr_call_classify.call_time' , 25],['>','asr_call_classify.call_time',0]]);
        $countQuery = clone $query;
        $pages = new Pagination(['totalCount' => $countQuery->count(), 'pageSize' => '1']);
        if (isset($_POST['pageSize'])) {//设置了参数，就把参数给pageSize session
            setcookie('pageSize', $_POST['pageSize']);
        }
        if (!empty($_COOKIE['pageSize'])) {
            $pages->pageSize = (int)$_COOKIE['pageSize'];
        } else {
            $pages->pageSize = 2;
        }
        $modelsp = $query->offset($pages->offset)
            ->limit($pages->limit)->asArray()
            ->all();
        return $this->render('deny', [
            'model' => $modelsp,
            'pages' => $pages,
            'id'=>$id,
        ]);

    }

    //未拨通用户
    public function actionNot($id)
    {
        $query = AsrCustomer::find()->select('asr_customer.customer_id,asr_customer.username,asr_customer.phone_num,asr_customer.company_name,asr_call_classify.call_number,asr_call_classify.call_time,asr_call_classify.created_at,asr_call_classify.updated_at')->joinWith('asrCallClassify',true,'LEFT JOIN')->where(['asr_call_classify.call_time'=>0]);
        $countQuery = clone $query;
        $pages = new Pagination(['totalCount' => $countQuery->count(), 'pageSize' => '1']);
        if (isset($_POST['pageSize'])) {//设置了参数，就把参数给pageSize session
            setcookie('pageSize', $_POST['pageSize']);
        }
        if (!empty($_COOKIE['pageSize'])) {
            $pages->pageSize = (int)$_COOKIE['pageSize'];
        } else {
            $pages->pageSize = 2;
        }
        $modelsp = $query->offset($pages->offset)
            ->limit($pages->limit)->asArray()
            ->all();
        return $this->render('not', [
            'model' => $modelsp,
            'pages' => $pages,
            'id'=>$id,
        ]);

    }
    /**
     * 下载当前用户列表全部号码 Excel格式
     */
    public function actionDownloadview($id)
    {
        $query = AsrCustomer::find()->select('asr_customer.customer_id,asr_customer.username,asr_customer.phone_num,asr_customer.company_name,asr_call_classify.call_number,asr_call_classify.call_time,asr_call_classify.created_at,asr_call_classify.updated_at')->joinWith('asrCallClassify',true,'LEFT JOIN')->where(['asr_customer.mobile_id'=>$id])->asArray()->all();
        //var_dump($query);
        Excel::export([
            'models' => $query,
            'fileName' => date('Y-m-d').'_'.'Export',
            'columns' => [
                'customer_id','username','phone_num','company_name','call_number','call_time',
                ['attribute' => 'created_at',
                    'value'=>
                        function($query){
                            return  date('Y-m-d H:i:s',$query['created_at']);   //主要通过此种方式实现
                        },
                ],
                ['attribute' => 'updated_at',
                    'value'=>
                        function($query){
                            return  date('Y-m-d H:i:s',$query['updated_at']);   //主要通过此种方式实现
                        },
                ],
            ],
            'headers'=>[
                'customer_id'=>'客户id',
                'username'=>'客户名',
                'phone_num'=>'手机号',
                'company_name'=>'公司名',
                'call_number'=>'拨打电话',
                'call_time'=>'拨打时间',
                'created_at'=>'创建时间',
                'updated_at'=>'更新时间',

            ],

        ]);
    }
    /**
     *潜力客户号码表下载
     */
    public function actionDownloadpotnetial($id)
    {
        $query = AsrCustomer::find()->select('asr_customer.customer_id,asr_customer.username,asr_customer.phone_num,asr_customer.company_name,asr_call_classify.call_number,asr_call_classify.call_time,asr_call_classify.created_at,asr_call_classify.updated_at')->joinWith('asrCallClassify',true,'LEFT JOIN')->where(['asr_customer.mobile_id'=>$id])->where(['and' , ['>' , 'asr_call_classify.call_time' , 45] ])->asArray()->all();
        Excel::export([
            'models' => $query,
            'fileName' => date('Y-m-d').'_'.'Export',
            'columns' => [
                'customer_id','username','phone_num','company_name','call_number','call_time',
                ['attribute' => 'created_at',
                    'value'=>
                        function($query){
                            return  date('Y-m-d H:i:s',$query['created_at']);   //主要通过此种方式实现
                        },
                ],
                ['attribute' => 'updated_at',
                    'value'=>
                        function($query){
                            return  date('Y-m-d H:i:s',$query['updated_at']);   //主要通过此种方式实现
                        },
                ],
            ],
            'headers'=>[
                'customer_id'=>'客户id',
                'username'=>'客户名',
                'phone_num'=>'手机号',
                'company_name'=>'公司名',
                'call_number'=>'拨打电话',
                'call_time'=>'拨打时间',
                'created_at'=>'创建时间',
                'updated_at'=>'更新时间',

            ],

        ]);
    }
    /**
     *普通客户号码表下载
     */
    public function actionDownloadcommon($id)
    {
        $query = AsrCustomer::find()->select('asr_customer.customer_id,asr_customer.username,asr_customer.phone_num,asr_customer.company_name,asr_call_classify.call_number,asr_call_classify.call_time,asr_call_classify.created_at,asr_call_classify.updated_at')->joinWith('asrCallClassify',true,'LEFT JOIN')->where(['asr_customer.mobile_id'=>$id])->where(['and' , ['>' , 'asr_call_classify.call_time' , 25],['<=','asr_call_classify.call_time' , 45]])->asArray()->all();
        Excel::export([
            'models' => $query,
            'fileName' => date('Y-m-d').'_'.'Export',
            'columns' => [
                'customer_id','username','phone_num','company_name','call_number','call_time',
                ['attribute' => 'created_at',
                    'value'=>
                        function($query){
                            return  date('Y-m-d H:i:s',$query['created_at']);   //主要通过此种方式实现
                        },
                ],
                ['attribute' => 'updated_at',
                    'value'=>
                        function($query){
                            return  date('Y-m-d H:i:s',$query['updated_at']);   //主要通过此种方式实现
                        },
                ],
            ],
            'headers'=>[
                'customer_id'=>'客户id',
                'username'=>'客户名',
                'phone_num'=>'手机号',
                'company_name'=>'公司名',
                'call_number'=>'拨打电话',
                'call_time'=>'拨打时间',
                'created_at'=>'创建时间',
                'updated_at'=>'更新时间',

            ],

        ]);
    }
    /**
     *拒绝客户号码表下载
     */
    public function actionDownloaddeny($id)
    {
        $query = AsrCustomer::find()->select('asr_customer.customer_id,asr_customer.username,asr_customer.phone_num,asr_customer.company_name,asr_call_classify.call_number,asr_call_classify.call_time,asr_call_classify.created_at,asr_call_classify.updated_at')->joinWith('asrCallClassify',true,'LEFT JOIN')->where(['and' , ['<=' , 'asr_call_classify.call_time' , 25],['>','asr_call_classify.call_time',0]])->asArray()->all();
        Excel::export([
            'models' => $query,
            'fileName' => date('Y-m-d').'_'.'Export',
            'columns' => [
                'customer_id','username','phone_num','company_name','call_number','call_time',
                ['attribute' => 'created_at',
                    'value'=>
                        function($query){
                            return  date('Y-m-d H:i:s',$query['created_at']);   //主要通过此种方式实现
                        },
                ],
                ['attribute' => 'updated_at',
                    'value'=>
                        function($query){
                            return  date('Y-m-d H:i:s',$query['updated_at']);   //主要通过此种方式实现
                        },
                ],
            ],
            'headers'=>[
                'customer_id'=>'客户id',
                'username'=>'客户名',
                'phone_num'=>'手机号',
                'company_name'=>'公司名',
                'call_number'=>'拨打电话',
                'call_time'=>'拨打时间',
                'created_at'=>'创建时间',
                'updated_at'=>'更新时间',

            ],

        ]);

    }
    /**
     *未接通用户
     */
    public function actionDownloadnot($id)
    {
        $query = AsrCustomer::find()->select('asr_customer.customer_id,asr_customer.username,asr_customer.phone_num,asr_customer.company_name,asr_call_classify.call_number,asr_call_classify.call_time,asr_call_classify.created_at,asr_call_classify.updated_at')->joinWith('asrCallClassify',true,'LEFT JOIN')->where(['asr_call_classify.call_time'=>0])->asArray()->all();
        Excel::export([
            'models' => $query,
            'fileName' => date('Y-m-d').'_'.'Export',
            'columns' => [
                'customer_id','username','phone_num','company_name','call_number','call_time',
                ['attribute' => 'created_at',
                    'value'=>
                        function($query){
                            return  date('Y-m-d H:i:s',$query['created_at']);   //主要通过此种方式实现
                        },
                ],
                ['attribute' => 'updated_at',
                    'value'=>
                        function($query){
                            return  date('Y-m-d H:i:s',$query['updated_at']);   //主要通过此种方式实现
                        },
                ],
            ],
            'headers'=>[
                'customer_id'=>'客户id',
                'username'=>'客户名',
                'phone_num'=>'手机号',
                'company_name'=>'公司名',
                'call_number'=>'拨打电话',
                'call_time'=>'拨打时间',
                'created_at'=>'创建时间',
                'updated_at'=>'更新时间',

            ],

        ]);
    }
    /**
     * Creates a new AsrCustomer model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate()
    {
        $model = new AsrCustomer();

        if ($model->load(Yii::$app->request->post()) ) {
            $mobile_id = $_GET['mobile_id'];
            $model->mobile_id = $mobile_id;
            $model->save();
            echo Yii::$app->session->setFlash('success','新增成功！');
            return $this->redirect(['index?mobile_id='.$mobile_id, 'id' => $model->customer_id]);
        }

        return $this->render('create', [
            'model' => $model,
        ]);
    }

    /**
     * Updates an existing AsrCustomer model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param integer $id
     * @return mixed
     * @throws NotFoundHttpException if the model cannot be found
     */
    public function actionUpdate($id)
    {
        $model = $this->findModel($id);

        if ($model->load(Yii::$app->request->post())) {
            $mobile_id = $_GET['mobile_id'];
            $model->mobile_id = $mobile_id;
            $model->save();
            echo Yii::$app->session->setFlash('success','修改成功！');
            return $this->redirect(['index?mobile_id='.$mobile_id, 'id' => $model->customer_id]);
        }

        return $this->render('update', [
            'model' => $model,
        ]);
    }

    /**
     * Deletes an existing AsrCustomer model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param integer $id
     * @return mixed
     * @throws NotFoundHttpException if the model cannot be found
     */
    public function actionDelete($id)
    {
        echo Yii::$app->session->setFlash('danger','删除成功！');
        $this->findModel($id)->delete();
        $mobile_id = $_GET['mobile_id'];
        return $this->redirect(['index?mobile_id='.$mobile_id]);
    }

    /**
     * Finds the AsrCustomer model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param integer $id
     * @return AsrCustomer the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id)
    {
        if (($model = AsrCustomer::findOne($id)) !== null) {
            return $model;
        }

        throw new NotFoundHttpException('The requested page does not exist.');
    }
}
